// pages/member/rankingListSecond/rankingListSecond.js
import {
  payKeyboardAsync
} from "../../../../mycomponent/payKeyboard/PayKeyboardAsync.js";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    // bool	转让倍数，为1则不限制
    transfer_multiple: 0,
    // 转让最小额，为0则不限制
    transfer_fetter: 0,
    //爱心转账值
    recharge_value: "",
    //爱心会员ID
    recharge_id: "",
    //输入搜索值
    recharge_search: "",
    // 转让手续费比率
    ratio: 0,
    // 实际到账数
    arrival_count: 0,
    buttonOff: true, //默认禁止提交
    love_name: "", //爱心值自定义名称
    usable: 0, // 登陆会员可用爱心值
    showSearch: false,
    search_list: [], //搜索会员ID结果
    actualVal: "", //实际到账值
    sell_value: "", //输入框的值
    isclick: true,

    memberInfo: {
      nickname: "",
      realname: ""
    },

    //支付键盘
    payKeyboardShow: false,
    //是否需要支付密码
    need_password: false,
    //是否设置支付密码
    has_password: false,
    key: '',
    transferName:'',
    makeName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.key) {
      this.setData({
        key: options.key
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isclick = true;
    this.getUsable();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  searchTransfer(val) {
    var reSearch = val.detail.replace(/\D/g, '');
    let urlStr = "";
    if (this.data.key == 'donate') {
      urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Member.Controllers.query.index");
      urlStr += "&recipient=" + reSearch;
    } else {
      urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.team-dividend-search.index");
      urlStr += "&search=" + reSearch;
    }
    if (reSearch) {
      this.setData({
        memberInfo: {
          nickname: "",
          realname: ""
        }
      });
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result === 1) {
            if (this.data.key == 'donate') {
              let data = res.data.data.member;
              if (data) {
                this.setData({
                  showSearch: true,
                  'memberInfo.nickname': data.username ? data.username : "",
                  'memberInfo.realname': data.realname ? data.realname : ""
                });
              }
            } else {
              var data = res.data.data[0];
              this.setData({
                showSearch: true,
                'memberInfo.nickname': data.username ? data.username : "",
                'memberInfo.realname': data.nickname ? data.nickname : ""
              });
            }

          } else {
            this.setData({
              showSearch: false
            });
            app.tips(res.data.msg);
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  searchTransfer_1() {
    var reSearch = this.data.recharge_search;
    if (reSearch) {
      this.setData({
        memberInfo: {
          nickname: "",
          realname: ""
        }
      });
      let urlStr = "";
      if (this.data.key == 'donate') {
        urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Member.Controllers.query.index");
        urlStr += "&recipient=" + reSearch;
      } else {
        urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.team-dividend-search.index");
        urlStr += "&search=" + reSearch;
      }
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          if (res.data.result === 1) {
            if (this.data.key == 'donate') {
              let data = res.data.data.member;
              if (data) {
                this.setData({
                  showSearch: true,
                  'memberInfo.nickname': data.username ? data.username : "",
                  'memberInfo.realname': data.realname ? data.realname : ""
                });
              }
            } else {
              var data = res.data.data[0];
              this.setData({
                showSearch: true,
                'memberInfo.nickname': data.username ? data.username : "",
                'memberInfo.realname': data.nickname ? data.nickname : ""
              });
            }
          } else {
            this.setData({
              showSearch: false
            });
            app.tips(res.data.msg);
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  // makeTransfer(event) {
  //   var jid = event.target.dataset.jid;
  //   var name = event.target.dataset.name;
  //   this.setData({
  //     recharge_id: jid,
  //     recharge_search: name,
  //     showSearch: false
  //   });
  // },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          if(res.data.data.need_sign) {
            wx.showModal({
              title: '提示',
              content: '请先完成合同签署',
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/packageH/sign_shopV2/index/index?scene=3',
                  });
                }
              }
            })
            return;
          }
          
          this.setData({
            usable: res.data.data.usable,
            love_name: res.data.data.love_name,
            ratio: res.data.data.transfer_proportion,
            transfer_multiple: res.data.data.transfer_multiple,
            transfer_fetter: res.data.data.transfer_fetter,
            need_password: res.data.data.transfer_password,
            has_password: res.data.data.has_password,
            point_name: res.data.data.point_name,
            
          });
          wx.setNavigationBarTitle({
            title: this.data.key == 'donate' ? res.data.data.love_name + this.data.point_name + '转赠' : res.data.data.love_name ? (res.data.data.love_name + this.data.transferName) : this.data.transferName
          });
          var recharge_value = this.data.recharge_value;
          var ratio = this.data.ratio;
          let basicinfo = wx.getStorageSync('yz_basic_info');
          let pluginSetting = basicinfo.plugin_setting;
          this.setData({
            actualVal: recharge_value - (recharge_value * ratio) / 100,
            transferName:pluginSetting.love.transfer,
            makeName:pluginSetting.love.transference,
          });
          
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  payKeyboardComplete(evt) {
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword() {
    this.setData({
      payKeyboardShow: true
    });
    return new Promise((resove, reject) => {
      payKeyboardAsync.addCompleteFn((pass) => {
        resove(pass);
      });
    });
    // let pass = await this.getPayKeyboardPassword();
    // console.log(pass);
  },
  payKeyboardClose() {
    this.setData({
      payKeyboardShow: false
    });
  },
  async getBalance() {
    //recipient 被转让者ID
    //change_value 转让爱心值
    if (!this.data.isclick) {
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      });
      return;
    }


    if (!this.data.recharge_search) {
      wx.showToast({
        title: "请输入会员ID/手机号/昵称",
        icon: 'none'
      });
      return;
    }
    if (!this.data.recharge_value) {
      wx.showToast({
        title: `请输入您要${this.data.transferName}的数额`,
        icon: 'none'
      });
      return;
    }
    if (!this.data.showSearch) {
      return app.tips('输入的值没有搜索到对应的用户');
    }

    if (this.data.need_password == true && this.data.has_password == false) {
      let confirmFlag = await app.confirm("请先设置支付密码");
      if (confirmFlag) {
        wx.navigateTo({
          url: '/packageA/member/set_balance_password/set_balance_password'
        });
      }
      return;
    }
    

    let urlStr = "";
    if (this.data.key == 'donate') {
      urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.transfer-point.index");
    } else {
      urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.transfer.index");
    }
    urlStr += "&recipient=" + this.data.recharge_search;
    urlStr += "&change_value=" + this.data.recharge_value;

    if (this.data.need_password == true) { //开启支付密码验证
      let pass = await this.getPayKeyboardPassword();
      urlStr += '&password=' + pass;
    }
    this.data.isclick = false;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
          this.setData({
            recharge_value: '',
            recharge_search: ""
          });
          setTimeout(() => {
            this.getUsable();
            // 返回上一页
            wx.navigateBack({
              delta: 1
            });
          }, 1000);

        } else {
          this.data.isclick = true;
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 爱心值
  onChange(val) {
    this.setData({
      recharge_value: val.detail
    });
  },
  // 会员ID
  onChange2(val) {
    this.setData({
      recharge_search: val.detail.replace(/\D/g, '')
    });
  }
});